<template>
  <div class="hello">
    <button type="button" @click="handlerEn">加密</button>
    <button type="button" @click="handlerDe">解密</button>
    <button type="button" @click="handlerRsa">生成钥匙</button>
    <p>
      加密前数据：
      <textarea rows="10" cols="100" v-model="encrydata">
      </textarea>
    </p>

    <p>
      加密后数据：
      <textarea rows="10" cols="100" v-model="resultData">
      </textarea>
    </p>

    <p>
      解密后数据：
      <textarea rows="10" cols="100" v-model="dencdata">
      </textarea>
    </p>
    <p>
      钥匙数据：
      <textarea rows="10" cols="100" v-model="rsaData">
      </textarea>
    </p>
    <!--<h3>Installed CLI Plugins</h3>
    <ul>
      <li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-babel" target="_blank" rel="noopener">babel</a></li>
      <li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-eslint" target="_blank" rel="noopener">eslint</a></li>
    </ul>
    <h3>Essential Links</h3>
    <ul>
      <li><a href="https://vuejs.org" target="_blank" rel="noopener">Core Docs</a></li>
      <li><a href="https://forum.vuejs.org" target="_blank" rel="noopener">Forum</a></li>
      <li><a href="https://chat.vuejs.org" target="_blank" rel="noopener">Community Chat</a></li>
      <li><a href="https://twitter.com/vuejs" target="_blank" rel="noopener">Twitter</a></li>
      <li><a href="https://news.vuejs.org" target="_blank" rel="noopener">News</a></li>
    </ul>
    <h3>Ecosystem</h3>
    <ul>
      <li><a href="https://router.vuejs.org" target="_blank" rel="noopener">vue-router</a></li>
      <li><a href="https://vuex.vuejs.org" target="_blank" rel="noopener">vuex</a></li>
      <li><a href="https://github.com/vuejs/vue-devtools#vue-devtools" target="_blank" rel="noopener">vue-devtools</a></li>
      <li><a href="https://vue-loader.vuejs.org" target="_blank" rel="noopener">vue-loader</a></li>
      <li><a href="https://github.com/vuejs/awesome-vue" target="_blank" rel="noopener">awesome-vue</a></li>
    </ul>-->
  </div>
</template>

<script>
// const fs = require('fs');
const Base64 = require('js-base64').Base64
// const NodeRsa = require('node-rsa')
import crypto from 'crypto'
import NodeRsa from 'node-rsa'
// const crypto = require('crypto');

// Base64.encode(this.pwd);//加密
// Base64.decode(this.pwd);//解密

export default {
  name: 'Encryption-A',
  props: {
  },
  data:function(){
    return{
      id:'1',
      encrydata:'',
      resultData:'',
      dencdata:'',
      rsaData:'',
    }
  },
  created() {
  },
  mounted() {
  },
  methods:{
    handlerRsa(){
      const key = new NodeRsa({ b: 2048 }); //生成2048位的密钥
      let publicDer = key.exportKey("pkcs8-public-pem");  //公钥
      let privateDer = key.exportKey("pkcs1-private-pem");//私钥
      crypto.publicEncrypt(publicDer, Base64.encode(this.encrydata));
      this.rsaData = publicDer + "\r\n" + privateDer;
    },
    handlerEn() {
      this.resultData = Base64.encode(this.encrydata);//加密
    },
    handlerDe() {
      this.dencdata = Base64.decode(this.resultData);//解密
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
